<?
$con = file_get_contents("http://electronics.shop.ebay.com.sg/MP3-Players-/15057/i.html");

$m = preg_match_all('/class="vip">(.*?)<\/a>.*?class=".*?g-b">S\$\s([0-9]*.*?)<\/.*?class="hidlb">Time left:.*?<span.*?>(.*?)<\//',$con,$prod,PREG_SET_ORDER);

$s = sizeof($prod);

echo $s.'<br>';

echo '<html><head></head><body><table width="514" border="1">
	<tr>
    <th width="334" scope="col1">PRODUCT</th>
    <th width="72" scope="col2">PRICE</th>
	<th width="150" scope="col3">Time Left</th>
	<th width="150" scope="col4">days</th>
	<th width="150" scope="col5">hours</th>
	<th width="150" scope="col6">min</th>
	
	</tr>';
foreach($prod as $k => $product)
{
	//echo "$product[1]----------$product[2]----$k<br>";
	
	$prod_list[$k] = $product[1];
	$price_list[$k] = $product[2];
	$time_list[$k] = $product[3];
	$dhm = explode("&#160;",$time_list[$k]);
			
	switch(count($dhm))
	{
		case 3: 
			$day = substr($dhm[0],0,(strlen($dhm[0])-1));
			$hour = substr($dhm[1],0,(strlen($dhm[1])-1));;
			$min = substr($dhm[2],0,(strlen($dhm[2])-1));;
			
			break;	
		case 2: 
			$day = 0;
			$hour = substr($dhm[0],0,(strlen($dhm[0])-1));;
			$min = substr($dhm[1],0,(strlen($dhm[1])-1));;
			break;	
		
		case 1: 
			
			$day = 0;
			$hour = 0;
			$min = substr($dhm[0],0,(strlen($dhm[0])-1));;
			break;	
		
	}
	echo '<tr>
    <th scope="col1">'.$prod_list[$k].'</th>
    <th scope="col2">$'.$price_list[$k].'</th>
	<th scope="col3">'.$time_list[$k].'</th>
	<th scope="col4">'.$day.'</th>
	<th scope="col5">'.$hour.'</th>
	<th scope="col6">'.$min.'</th>
	</tr>  '; 
	$expire_time[$k] = ((time()) + ($day * 24 * 60 * 60) + ($hour * 60 * 60) + ($min * 60));
	echo "$expire_time[$k]";
	
	
}
echo '</table></body></html>';

 
include("sqlconnect.php");
 

for($i=0;$i<$m;$i++)
{

$brand_array["Apple"] = "apple";
$brand_array["Creative"] = "creative";
$brand_array["Microsoft"] = "microsoft";
$brand_array["BikeMan"] = "bikeman";


	foreach($brand_array as $brand_add => $brand_testname)  
	{
		$str=stristr($prod_list[$i],$brand_testname);
		$entertime = date('h:i:s');
		echo $entertime;
		if((strlen($str))>0)
		{
			//echo ($str."<br>");
			$qry= "INSERT INTO `product` (`product_name`, `product_price`,`brand_name`,`product_category`,`vendor_name`,`product_visibility`,`deal_type`,`expire_time`) 
			VALUES ('".$prod_list[$i]."', '".$price_list[$i]."','".$brand_add."','Consumer Electronics','ebay','1','2','".$entertime."')";
			break;
		}
		if(!next($brand_array))
		{
			$qry= "INSERT INTO `product` (`product_name`, `product_price`,`brand_name`,`product_category`,`vendor_name`,`product_visibility`,`deal_type`,`expire_time`) 
			VALUES ('".$prod_list[$i]."', '".$price_list[$i]."','Other','Consumer Electronics','ebay','1','2','".$entertime."')";
			break;	
		}
	}	
		 			
	$result = mysql_query($qry);
	
}
	
?>